package com.forchange.exe1
import org.apache.spark.util.AccumulatorV2

/**
 * @Author chencong
 * @Description 自定义累加器
 * @Date 9:39 下午 2021/6/26
 * @Param
 * */
class Accumulator extends AccumulatorV2[Int, Int] {
  //累加的结果（属性）
  private var sum = 10

  //当前累加器是否归为0
  override def isZero: Boolean = sum == 0

  //创建当前累加器的一个副本
  override def copy(): AccumulatorV2[Int, Int] = new Accumulator

  //重置为0
  override def reset(): Unit = sum = 0

  //累加
  override def add(v: Int): Unit = sum += v

  //将other的值合并到当前累加器
  override def merge(other: AccumulatorV2[Int, Int]): Unit = sum += other.value

  //访问最终累加结果
  override def value: Int = sum
}



